import dayjs from 'dayjs';
import { EMPTY_VALUE_LIST, LOTTERY_WIN_RATE, LOTTERY_ACTIVITY_TYPE } from '../constants/lottery';
import { DEFAULT_BG_LIST, ACTIVITY_TYPE } from '../../constants/common';
import { DELIVERY_TYPE_VALUE, SALES_TYPE_VALUE } from '../../constants/activity';
import { deepClone, formatDecimal, getTimeDuration } from '../../common/utils';

/**
 * 获取一个随机的提示信息
 */
export const getOneEmptyValue = () => {
    const idx = Math.floor(Math.random() * 4);
    const value = EMPTY_VALUE_LIST[idx];
    const { index, text, pic } = value;
    return {
        index,
        award: { name: text, pic },
    };
};

/**
 * 处理奖品数据
 * @param {Array} awardList 奖品列表
 */
export const dealLotteryAwardData = (awardList) => {
    // 4 - 7 个商品
    const len = awardList.length;
    let emptyIdxList = [];
    switch(len) {
        case 4:
            emptyIdxList = [1, 3, 5, 7];
            break;
        case 5:
            emptyIdxList = [1, 5, 7];
            break;
        case 6:
            emptyIdxList = [1, 5];
            break;
        case 7:
            emptyIdxList = [1];
            break;
    }
    let resultList = [];
    let count = 0;
    let emptyCount = 0;
    for (let i = 0; i < 9; i++) {
        if (emptyIdxList.indexOf(i) > -1) {
            resultList.push({
                isStart: false,
                isEmpty: true,
                value: i + 1,
                prize: null,
                award: {
                    name: EMPTY_VALUE_LIST[emptyCount].text,
                    pic: EMPTY_VALUE_LIST[emptyCount].pic,
                },
            });
            emptyCount++;
        } else if (i == 4) {
            // 开始按钮
            resultList.push({
                isStart: true,
                isEmpty: false,
                value: i + 1,
                award: null,
            });
        } else {
            resultList.push({
                isStart: false,
                isEmpty: false,
                value: i + 1,
                prize: count + 1,
                award: {
                    name: awardList[count]?.productName,
                    pic: awardList[count]?.productPics[0],
                },
            });
            count++;
        }
    }
    return resultList;
};

/**
 * 处理大转盘奖品数据
 * @param {Array} awardList 奖品列表
 */
 export const dealWheelLotteryAwardData = (awardList) => {
    // 4 - 7 个商品
    const len = awardList.length;
    let emptyIdxList = [];
    switch(len) {
        case 4:
            emptyIdxList = [1, 3, 5, 7];
            break;
        case 5:
            emptyIdxList = [1, 3, 5];
            break;
        case 6:
            emptyIdxList = [1, 3];
            break;
        case 7:
            emptyIdxList = [1];
            break;
    }
    let resultList = [];
    let count = 0;
    let emptyCount = 0;
    for (let i = 0; i < 8; i++) {
        if (emptyIdxList.indexOf(i) > -1) {
            resultList.push({
                isStart: false,
                isEmpty: true,
                value: i + 1,
                prize: null,
                award: {
                    name: EMPTY_VALUE_LIST[emptyCount].text,
                    pic: EMPTY_VALUE_LIST[emptyCount].pic,
                },
            });
            emptyCount++;
        } else {
            resultList.push({
                isStart: false,
                isEmpty: false,
                value: i + 1,
                prize: count + 1,
                award: {
                    name: awardList[count]?.productName,
                    pic: awardList[count]?.productPics[0],
                },
            });
            count++;
        }
    }
    return resultList;
};

/**
 * 处理翻牌奖品数据
 * @param {Array} awardList 奖品列表
 * @param {Number} prize 几等奖
 */
export const dealFlipLotteryAwardData = (awardList, prize) => {
    const { productName, productPics } = awardList[prize - 1];
    return {
        name: productName,
        pic: productPics[0],
    };
};

/**
 * 获取奖品中奖率
 * @param {String} type 
 * @param {Number} len 
 */
 export const getProbability = (type = LOTTERY_WIN_RATE.easy, len) => {
    let total, arr = [], rate = parseInt(type);
    for (let i = 0; i < len; i ++) {
        arr.push(i + 1);
    }
    total = arr.reduce((i, m) => i + m, 0);
    return arr.map(i => formatDecimal(i / total * rate * 10, 0));
};

/**
 * 获取抽奖次数
 */
export const getFrequency = () => {
    let arr = [];
    for (let i = 0; i < 24; i ++) {
        const value = i + 1;
        arr.push({
            value,
            name: `${value}次`,
        });
    }
    return arr;
};

/**
 * 处理默认抽奖活动信息
 * @param ownerInfo 拥有者信息
 */
 export const dealLotteryDefaultData = (ownerInfo = {}) => {
    return {
        name: '',
        type: ACTIVITY_TYPE.lottery,
        tel: ownerInfo.tel,
        background: DEFAULT_BG_LIST[0],
        intro: [
            { type: 'text', resource: '' }
        ],
        activityProductWithSkus: [],
        warehouseIds: [],
        saleMode: SALES_TYPE_VALUE.only_brand_selling,
        deliveryModes: [DELIVERY_TYPE_VALUE.express],
        maxiumPurchaseFrequency: 1,
        begined: dayjs().startOf('minute').valueOf(),
        ended: dayjs().add(6, 'day').startOf('minute').valueOf(),
        timeRanges: [],
        lottery1: {
            uiType: LOTTERY_ACTIVITY_TYPE.fruit,
            winRate: LOTTERY_WIN_RATE.easy,
            prizeRates: [],
        },
    };
};

/**
 * 处理活动保存数据
 * @param {Object} activityInfo 
 */
 export const dealLotterySaveData = (activityInfo) => {
    activityInfo = deepClone(activityInfo);
    const { intro } = activityInfo;
    // 描述
    activityInfo.intro = intro.filter(i => i.resource);
    // 数据格式处理
    activityInfo.maxiumPurchaseFrequency !== undefined && (activityInfo.maxiumPurchaseFrequency -= 0);
    activityInfo.activityProductWithSkus = activityInfo.activityProductWithSkus.map(ps => {
        ps.skus = ps.skus.map(s => {
            s.price -= 0;
            s.maximum -= 0;
            return s;
        });
        return ps;
    });
    activityInfo.unifiedShippingFee = 1;
    activityInfo.lottery1.winRate -= 0;
    activityInfo.lottery1.prizeRates = activityInfo.lottery1.prizeRates.map(p => p -= 0);
    // 删除数据
    delete activityInfo.activityId;
    delete activityInfo.brandId;
    delete activityInfo.pubUserId;
    delete activityInfo.qualifyTypes;
    delete activityInfo.processStatus;
    delete activityInfo.published;
    delete activityInfo.verifyQualification;
    delete activityInfo.owner;
    delete activityInfo.pics;
    return activityInfo;
};

/**
 * 获取抽奖活动中奖人
 * @param {Array} list 
 */
export const dealAwardReporterListData = (list) => {
    return list.map(item => {
        const { userNickName, desc, awarded } = item;
        return {
            text: `${userNickName} ${desc}`,
            time: `${getTimeDuration(awarded, '前')}`,
        };
    });
};
